﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using DTO;
using System.IO;
using System.Web.Caching;
using System.Collections;
using System.Web.SessionState;
using System.Configuration;
namespace Models
{
    public static class ManagerSessionCache
    {
        public const string LOGIN = "login";
        public static int ChangeAppSetting(string key, string value)
        {
            try
            {

                Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
                config.AppSettings.Settings[key].Value = value;
                config.Save();
                return 1;
            }
            catch { }
            return 0;
        }

        public static void SetSessionLogin(LoginResult item) {
            System.Web.SessionState.HttpSessionState session = System.Web.HttpContext.Current.Session;
            item.sessionId = session.SessionID; 
            session[LOGIN] = item;
        }

        public static void RemoveSession(String sessionName)
        {
            System.Web.SessionState.HttpSessionState session = System.Web.HttpContext.Current.Session;
            if (session[sessionName] != null)
            {
                session[sessionName] = null;
                ManagerSessionCache.SetSessionLogin(null);
                session.Remove(sessionName);
                session.Clear();
                session.Abandon();
            }
        }

        public static LoginResult GetSessionLogin() {
            System.Web.SessionState.HttpSessionState session = System.Web.HttpContext.Current.Session;
            LoginResult item = null;
            if (session[LOGIN] != null) {
                item = (LoginResult)session[LOGIN];
            }
            return item;
        }
    }
}
